import axios from "axios";

export default {
    namespaced: true, // 开启命名空间
    actions: {
        addPersonFromGitHub(context, value) {
            if (!value.isEmpty) {
                axios.get(`https://api.github.com/search/users?q=${value}`).then(
                    response => {
                        let users = response.data.items;
                        console.log(users)
                        let ps = []
                        users.forEach((user) => {
                            ps.unshift({id: user.id, name: user.login})
                        })
                        context.commit('ADD_PERSONS', ps);
                    },
                    error => {
                        alert('请求失败：' + error.message)
                    }
                )
            } else {
                alert('搜索数据不能为空')
            }
        }
    },
    mutations: {
        ADD_PERSON(state, value) {
            state.persons.unshift(value);
        },
        ADD_PERSONS(state, value) {
            value.forEach((p) => {
                state.persons.unshift(p);
            })
        }
    },
    state: {
        persons: [
            {id: '001', name: '张三'}
        ],
    },
    getters: {},
}